<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-30
 * Time: 11:12
 */

namespace app\api\service\store;


use app\api\model\StoreOrder;
use app\api\model\User;
use app\api\service\Token;
use app\lib\exception\ParameterException;

class StaffPayOrderSer extends Token
{
    /**
     *  1.传入的判断数据
     *  1.判断是否是取送员
     *  2.判断订单信息以及 取送员是否一致
     *  3.修改订单状态
     */

    // 订单信息
    private $order;

    // 取送员信息
    private $staff;

    function __construct($orderId)
    {
        $this->order = StoreOrder::get($orderId);
        $user = User::get(self::getCurrentUid());
        $this->staff = Token::judgeStaff($user);
    }

    // 入口方法
    public function entrance()
    {
        return $this->judgeData();
    }

    // 判断订单 与取送员信息
    private function judgeData()
    {
        if(!$this->order) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '订单不存在'
                ]
            ]);
        }
        if($this->order['status'] != 3) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '订单状态错误'
                ]
            ]);
        }
        if($this->order['pick_deliver_staff_id'] != $this->staff['id']) {
            throw new ParameterException([
                'msg' => [
                    'store_order' => '取送员与订单不匹配'
                ]
            ]);
        }
        return $this->updateOrder();
    }

    // 更新订单
    private function updateOrder()
    {
        $storeOrder = new StoreOrder();
        $storeOrder->save([
            'payment' => 3,
            'status' => 4,
            'pay_time' => time()
        ],[ 'id'=> $this->order['id']]);
        return writeJson(202, [], '线下支付提交成功！');
    }
}